package com.zhuifeng.product.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zhuifeng.common.core.domain.R;
import io.swagger.models.auth.In;
import org.redisson.Redisson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zhuifeng.common.log.annotation.Log;
import com.zhuifeng.common.log.enums.BusinessType;
import com.zhuifeng.common.security.annotation.RequiresPermissions;
import com.zhuifeng.product.domain.Type;
import com.zhuifeng.product.service.ITypeService;
import com.zhuifeng.common.core.web.controller.BaseController;
import com.zhuifeng.common.core.web.domain.AjaxResult;
import com.zhuifeng.common.core.utils.poi.ExcelUtil;

/**
 * 类型Controller
 *
 * @author zhuifeng
 * @date 2022-08-17
 */
@RestController
@RequestMapping("/type")
public class TypeController extends BaseController
{
    @Autowired
    private ITypeService typeService;

    /**
     * 查询类型下模板
     */
    @RequiresPermissions("product:type:getModuleMap")
    @GetMapping("/getModuleMap/{typeId}")
    public R<Map<String ,Object>> getModuleMap(@PathVariable("typeId")String typeId)
    {
        Map<String, Object> typeMap = typeService.getModuleMap(typeId);
        return R.ok(typeMap);
    }

    /**
     * 查询类型列表
     */
    @RequiresPermissions("product:type:getTypeMap")
    @GetMapping("/getTypeMap")
    public R<Map<String ,List<Type>>> getTypeMap()
    {
        startPage();
        Map<String, List<Type>> typeMap = typeService.getTypeMap();
        return R.ok(typeMap);
    }

    /**
     * 查询类型列表
     */
    @RequiresPermissions("product:type:list")
    @GetMapping("/list")
    public AjaxResult list(Type type) throws InterruptedException {
        List<Type> list = typeService.selectTypeList();
        return AjaxResult.success(list);
    }

    /**
     * 导出类型列表
     */
    @RequiresPermissions("product:type:export")
    @Log(title = "类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Type type) throws InterruptedException {
        List<Type> list = typeService.selectTypeList();
        ExcelUtil<Type> util = new ExcelUtil<Type>(Type.class);
        util.exportExcel(response, list, "类型数据");
    }

    /**
     * 获取类型详细信息
     */
    @RequiresPermissions("product:type:query")
    @GetMapping(value = "/{typeId}")
    public AjaxResult getInfo(@PathVariable("typeId") String typeId)
    {
        return AjaxResult.success(typeService.selectTypeByTypeId(typeId));
    }

    /**
     * 新增类型
     */
    @RequiresPermissions("product:type:add")
    @Log(title = "类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Type type)
    {
        return toAjax(typeService.insertType(type));
    }

    /**
     * 修改类型
     */
    @RequiresPermissions("product:type:edit")
    @Log(title = "类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Type type)
    {
        return toAjax(typeService.updateType(type));
    }

    /**
     * 删除类型
     */
    @RequiresPermissions("product:type:remove")
    @Log(title = "类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{typeIds}")
    public AjaxResult remove(@PathVariable String[] typeIds)
    {
        return toAjax(typeService.deleteTypeByTypeIds(typeIds));
    }
}
